/**
 * 
 */
package com.fresh.utils;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * @author 作者姓名 E-mail: email地址
 * @version 创建时间：2017年10月26日 上午11:37:50
 * 类说明
 */
/**
 * @author Administrator
 *
 */
public class Base64Utils {
	public static void main(String[] args)  
    {  
        String imgString="/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCABkAMgDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAA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GlfGdkY4G5sn1JwMsQCAblFFFABRRRQAVHPMttbyzuJCkaF2EcbOxAGeFUEsfYAk9qkooAp6VqK6tpdvfpbXdsJk3eRdwNDLGehVkbkEH8D1BIINXKKKACiiq99f2emWcl5f3cFpax43zTyCNFyQBljwMkgfjQBYoqOCeG6t4ri3ljmglQPHJGwZXUjIII4II5zUlABRRRQAUVn6JomneHNHg0nSbf7PYwbvLi3s+3cxY8sSTySeTWhQBHPG01vLEk0kDuhVZYwpZCR94bgRkdeQR6g1z/AIb0XRfAHhrTdHSe0tgzpD5rkRG7uWHJ+Ykl2I4XJIAAHAFdJVe6sLO+8j7ZaQXHkSrPD50Yfy5F+6656MMnBHIoAsUUUUAFFFFAEc8bTW8sSTSQO6FVljClkJH3huBGR15BHqDUdhZpp+nW1lEcx28SRKdipkKAB8qAKOnRQAOwAqxRQAVXvftnkL9h8jzvNj3efnb5e9fMxj+LZu29t2M8ZqxRQAUVXe136jDefaJ18qKSLyVfEb7yh3MvdhswD2DN61YoAKKKKACiq81vLJeW06Xs8UcW7fAgQpNkYG4lSwx1G0r75HFWKACiisf/AIRuz/4TL/hKPMn+3f2f/Z/l7h5fl+Z5mcYzuz3zjHagDYqOeCG6t5be4ijmglQpJHIoZXUjBBB4II4xVPVbTUbv7F/Z2qfYPJu45bj/AEdZftEIzui5+7u4+YcjFaFABRRRQBTutVsbK/sbC4uY47u/d0tYT96UohdsD0Cjk9OQOpANyq81hZ3F5bXk1pBJdWu77PM8YLxbhhtrHlcjg461YoAKKKKAI4xMHmMskbIXzEFQqVXaOGOTuO7ccjHBAxxkyUUUAFFV3huG1GGZbrbapFIklv5YPmOShV93UbQrjHff7CrFABRRRQAVXhv7O4vLmzhu4JLq12/aIUkBeLcMruUcrkcjPWrFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQB/9k=";
        GenerateImage(imgString);  
    }  
    //图片转化成base64字符串  
    public static String GetImageStr()  
    {//将图片文件转化为字节数组字符串，并对其进行Base64编码处理  
        String imgFile = "D:\\360CloudUI\\tupian\\jt.jpg";//待处理的图片  
        InputStream in = null;  
        byte[] data = null;  
        //读取图片字节数组  
        try   
        {  
            in = new FileInputStream(imgFile);          
            data = new byte[in.available()];  
            in.read(data);  
            in.close();  
        }   
        catch (IOException e)   
        {  
            e.printStackTrace();  
        }  
        //对字节数组Base64编码  
        BASE64Encoder encoder = new BASE64Encoder();  
        return encoder.encode(data);//返回Base64编码过的字节数组字符串  
    }  
      
    //base64字符串转化成图片  
    public static String GenerateImage(String imgStr)  
    {   //对字节数组字符串进行Base64解码并生成图片  
        if (imgStr == null) //图像数据为空  
            return "图像数据为空 ";  
        BASE64Decoder decoder = new BASE64Decoder();  
        try   
        {  
            //Base64解码  
            byte[] b = decoder.decodeBuffer(imgStr);  
            for(int i=0;i<b.length;++i)  
            {  
                if(b[i]<0)  
                {//调整异常数据  
                    b[i]+=256;  
                }  
            }  
            //生成jpeg图片  
            String name=DateUtil.getDateTime16String();
            String chsiimg = PropertiesUtils.getProperty("img_url");
            String imgFilePath =chsiimg +name+".jpg" ;//新生成的图片  
            OutputStream out = new FileOutputStream(imgFilePath);      
            out.write(b);  
            out.flush();  
            out.close();
            return name;  
        }   
        catch (Exception e)   
        {  
            return null;  
        }  
    }  

}
